Cosmos DB (1 / 62): Complete the following pre-trigger that adds timestampt to every new item:
function validateToDoItemTimestamp() {
/* Code here */
// item to be created in the current operation
var itemToCreate = /* Code here */;
// validate properties
if (!("timestamp" in itemToCreate)) {
var ts = new Date();
itemToCreate["timestamp"] = ts.getTime();
}
// update the item that will be created
/* Code here */
}
Answer:
function validateToDoItemTimestamp() {
var context = getContext();
var request = context.getRequest();
// item to be created in the current operation
var itemToCreate = request.getBody();
// validate properties
if (!("timestamp" in itemToCreate)) {
var ts = new Date();
itemToCreate["timestamp"] = ts.getTime();
}
// update the item that will be created
request.setBody(itemToCreate);
}